package classReflectDemo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 通过反射 观察泛型
 * 泛型在运行期不会检查参数类型，只存在编译期
 * @author jie
 *
 */
public class TestGeneric {
	public static void main(String[] args) {
		List list = new ArrayList();
		List<String> list2 = new ArrayList<String>();

		System.out.println(list.getClass() == list2.getClass());

		// ------通过普通方式添加
		list2.add("");
		// list2.add(11); //编译时 检测

		// -----反射添加
		Class clazz = list2.getClass();
		try {
			//List 继承collection 的add 方法
			//泛型在运行期不会检查参数类型，只存在编译期
			//报错  java.lang.NoSuchMethodException
//			Method method = clazz.getMethod("add", String.class);
			
			Method method = clazz.getMethod("add", Object.class);

			
			method.invoke(list2, "join");
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
